package com.lonwin.inspectionmanagement.mvp.model;

import android.app.Application;

import com.google.gson.Gson;
import com.jess.arms.integration.IRepositoryManager;
import com.jess.arms.mvp.BaseModel;

import com.jess.arms.di.scope.ActivityScope;

import javax.inject.Inject;

import com.lonwin.inspectionmanagement.mvp.contract.LookPatrolRecordContract;
import com.lonwin.inspectionmanagement.mvp.model.api.service.BusinessService;
import com.lonwin.inspectionmanagement.mvp.model.entity.bean.SignSuccessBean;
import com.lonwin.inspectionmanagement.mvp.model.entity.bean.XcjlbInfoBean;

import java.io.File;
import java.util.Map;

import io.reactivex.Observable;
import io.reactivex.functions.Function;
import okhttp3.MediaType;
import okhttp3.MultipartBody;
import okhttp3.RequestBody;


/**
 * ================================================
 * Description:
 * <p>
 * Created by MVPArmsTemplate on 12/25/2018 00:19
 * <a href="mailto:jess.yan.effort@gmail.com">Contact me</a>
 * <a href="https://github.com/JessYanCoding">Follow me</a>
 * <a href="https://github.com/JessYanCoding/MVPArms">Star me</a>
 * <a href="https://github.com/JessYanCoding/MVPArms/wiki">See me</a>
 * <a href="https://github.com/JessYanCoding/MVPArmsTemplate">模版请保持更新</a>
 * ================================================
 */
@ActivityScope
public class LookPatrolRecordModel extends BaseModel implements LookPatrolRecordContract.Model {
    @Inject
    Gson mGson;
    @Inject
    Application mApplication;

    @Inject
    public LookPatrolRecordModel(IRepositoryManager repositoryManager) {
        super(repositoryManager);
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        this.mGson = null;
        this.mApplication = null;
    }

    @Override
    public Observable<XcjlbInfoBean> getXcjlbById(String url, Map<String, Object> params) {
        return mRepositoryManager.obtainRetrofitService(BusinessService.class).getXcjlbById(url, params);
    }

    @Override
    public Observable<XcjlbInfoBean> submittedXcjlb(String url, Map<String, String> params) {
        return mRepositoryManager.obtainRetrofitService(BusinessService.class).submittedXcjlb(url, params);

    }

    @Override
    public Observable<String> uploadFile(String url, File file) {
        RequestBody requestFile =
                RequestBody.create(MediaType.parse("application/otcet-stream"), file);
        MultipartBody.Part body =
                MultipartBody.Part.createFormData("file", file.getName(), requestFile);
        return mRepositoryManager.obtainRetrofitService(BusinessService.class).uploadFile(url, body);
    }

    @Override
    public Observable<SignSuccessBean> signByHeZhang(String url, Map<String, String> params) {
        return mRepositoryManager.obtainRetrofitService(BusinessService.class).postMethodData(url,params)
                .map(new Function<String, SignSuccessBean>() {
                    @Override
                    public SignSuccessBean apply(String s) throws Exception {
                        return mGson.fromJson(s,SignSuccessBean.class);
                    }
                });
    }
}